//
//  UITableView+PlaceholdView.m
//  beichoo_N_ios
//
//  Created by fanfan on 2017/9/14.
//  Copyright © 2017年 陈健. All rights reserved.
//

#import "UITableView+PlaceholdView.h"
#import <objc/runtime.h>

@implementation NSObject (swizzle)
+ (void)swizzleInstanceSelector:(SEL)originalSel WithSwizzledSelector:(SEL)swizzledSel {
    Method originMethod = class_getInstanceMethod(self, originalSel);
    Method swizzedMehtod = class_getInstanceMethod(self, swizzledSel);
    BOOL methodAdded = class_addMethod(self, originalSel, method_getImplementation(swizzedMehtod), method_getTypeEncoding(swizzedMehtod));
    if (methodAdded)
    {
        class_replaceMethod(self, swizzledSel, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
    }else{
        method_exchangeImplementations(originMethod, swizzedMehtod);
    }
}
@end


@implementation UITableView (PlaceholdView)
+ (void)load
{
     static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleInstanceSelector:@selector(reloadData) WithSwizzledSelector:@selector(ff_reloadData)];
    });
}
- (void)setPlaceHolderView:(UIView *)placeHolderView
{
    objc_setAssociatedObject(self, @selector(placeHolderView), placeHolderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)placeHolderView
{
    return objc_getAssociatedObject(self, @selector(placeHolderView));
}
- (void)ff_reloadData
{
    [self ff_checkEmpty];
    [self ff_reloadData];
}
- (void)ff_checkEmpty
{
    BOOL isEmpty = YES;
    id<UITableViewDataSource> src = self.dataSource;
    NSInteger sections = 1;
    if ([src respondsToSelector:@selector(numberOfSectionsInTableView:)])
    {
        sections = [src numberOfSectionsInTableView:self];
    }
     for (int i = 0; i < sections; i++)
        {
            @autoreleasepool
            {
                NSInteger rows = [src tableView:self numberOfRowsInSection:i];
                if (rows)
                {
                    isEmpty = NO;
                }
            }
           }
    if (isEmpty)
    {
        [self.placeHolderView removeFromSuperview];
        [self addSubview:self.placeHolderView];
    }else{
        [self.placeHolderView removeFromSuperview];
    }
}

@end
